package org.flexlancer.databases
{
	import ENUMS.ApplicationVariables;
	
	import flash.utils.Dictionary;
	
	import org.flexlancer.utils.DateTimeUtils;
	
	public class ResetBill
	{
		public static const TABLE:String = "resetbill";

		public static var LastInsertRowID:Number = 0;

		public static function Add(server:String,
							billcategory:String,
							slotid:Number,
							total:Number,
							reason:String,
							kitchenqt:Number,
							items:Array
							):void
		{
			try{
				DatabaseManager.Manager.Begin();
				if ( items == null ) items = new Array();
				
				var insertBill:Object = new Object();
				var dtNow:Date = DateTimeUtils.TodayCutOffDate;

				insertBill.server = server;
				insertBill.billdate = DateTimeUtils.ToStandardDate(dtNow);
				insertBill.billtime = DateTimeUtils.ToStandardTime(dtNow);
				insertBill.total = total;
				insertBill.billcategory = billcategory;
				insertBill.slotid = slotid;
				insertBill.reason = reason;
				insertBill.user = ApplicationVariables.LOGIN_USER;
				
				insertBill.itemscount = items.length;
				insertBill.kitchenqt = kitchenqt;
				DatabaseManager.Manager.Insert(TABLE,insertBill);
				
				var billid:Number = DatabaseManager.Manager.LastInsertRowID;
				LastInsertRowID = DatabaseManager.Manager.LastInsertRowID;
				
				for each ( var singleItem:Object in items ){
					ResetBillItems.Add(billid,singleItem.code,singleItem.qty,singleItem.price,singleItem.total,singleItem.title,singleItem.cat);
				}
				
				DatabaseManager.Manager.Commit();
				
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
		
		public static function AddObject(parmObject:Object):void{
			try{
				//DatabaseManager.Manager.Begin();
				if ( items == null ) items = new Array();
				
				var items:Array = parmObject.items as Array;
				var insertBill:Object = parmObject;
				
				delete insertBill.billid;
				delete insertBill.items;
				
				DatabaseManager.Manager.Insert(TABLE,insertBill);
				
				var billid:Number = DatabaseManager.Manager.LastInsertRowID;
				LastInsertRowID = DatabaseManager.Manager.LastInsertRowID;
				
				for each ( var singleItem:Object in items ){
					singleItem.billrefid = billid;
					BillItems.AddObject(singleItem);
				}
				
				//DatabaseManager.Manager.Commit();
				
			}catch(err:Error){
				//DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
		public static function DropBill(billid:Number, dropReason:String ):void{
			try{
				var updateObj:Object = new Object;
				updateObj.billstatus = "DROP";
				updateObj.statusreason = dropReason;
				DatabaseManager.Manager.Update(TABLE,updateObj,"billid="+billid);
			}catch(err:Error){
				throw err;
			}
		}
		public static function List(condition:String = ""):Array{
			try{
				var arrBills:Array = DatabaseManager.Manager.Select(TABLE,null,condition);
				var hashTbl:Dictionary = new Dictionary();
				var ids:String = "";
				for each ( var singleBill:Object in arrBills ){
					if ( ids != "" ) ids += ",";
					hashTbl[singleBill.resetbillid] = singleBill;
					ids += singleBill.resetbillid;
					//singleBill.items = BillItems.List("billrefid="+singleBill.billid);
				}
				var sitems:Array = ResetBillItems.List("billrefid IN ("+ids+")");
				for each ( var singleP:Object in sitems ){
					if ( hashTbl[singleP.billrefid].items == undefined ){
						hashTbl[singleP.billrefid].items = new Array;
					}
					(hashTbl[singleP.billrefid].items as Array).push(singleP);
				}
				return arrBills;
			}catch(err:Error){
				throw err;
			}
			return new Array;
		}		
	}
}